package com.cn.redis.service;

import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cn.redis.serialize.StringSerializerExtend;
import com.cn.vo.User;

//@Service
public class UserService extends BaseRedisService<String, User>{
	
	private RedisSerializer<String> keySerializer;
	
	public UserService(){
		this.keySerializer = new StringSerializerExtend("user.");
	}
	
	@Transactional
	public void testSave(final User user){
		boolean result = getRedisTemplate().execute(new RedisCallback<Boolean>() {
			public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
				RedisSerializer<String> serializer = getKeySerializer();
				byte[] k = serializer.serialize(user.getId());
				byte[] v = getValueSerializer().serialize(user);
				
				if (v != null) {
					connection.set(k, v);
					connection.expire(k, 100000);
				}
				String s = null;
				//s.equals("");
				return true;
			}
		});
	}
	
	public RedisSerializer<String> getKeySerializer(){
		return keySerializer;
	}
	
}
